from pyecharts.charts import Timeline, Bar
from pyecharts import options as opts
from datetime import datetime

tasks = [
    {"Task": "公司后台系统1", "Start": '2023-10-01', "Finish": '2023-12-01'},
    {"Task": "公司后台系统2", "Start": '2023-10-15', "Finish": '2024-01-15'},
    {"Task": "公司后台系统3", "Start": '2023-11-01', "Finish": '2024-02-01'},
    {"Task": "公司后台系统4", "Start": '2023-11-15', "Finish": '2024-02-15'},
    {"Task": "小程序1", "Start": '2023-10-01', "Finish": '2023-11-01'},
    {"Task": "小程序2", "Start": '2023-10-15', "Finish": '2023-11-15'},
    {"Task": "小程序3", "Start": '2023-11-01', "Finish": '2023-12-01'},
    {"Task": "小程序4", "Start": '2023-11-15', "Finish": '2023-12-15'}
]

timeline = Timeline()

for task in tasks:
    start_date = datetime.strptime(task["Start"], "%Y-%m-%d")
    finish_date = datetime.strptime(task["Finish"], "%Y-%m-%d")
    days = (finish_date - start_date).days + 1

    bar = (
        Bar()
        .add_xaxis([task["Task"]])
        .add_yaxis("", [days], category_gap="60%")
        .set_global_opts(
            title_opts=opts.TitleOpts(title=f"{task['Task']} 扩展计划"),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
            yaxis_opts=opts.AxisOpts(name="天数")
        )
    )

    timeline.add(bar, task["Task"])

timeline.render("system_expansion_plan.html")  # 渲染为HTML文件
